<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use App\Http\Requests\Wechat\WallRequest;
use App\Repositories\BLL\WallBLL;

use EasyWeChat\Foundation\Application;
use Storage;

class WallController extends Controller
{
    private $wallBLL;
    private $currentRouteName;
    private $qrcode;

    public function __construct(WallBLL $wall, Application $wechat)
    {
        $this->currentRouteName = Route::currentRouteName();
        $this->wallBLL = $wall;
        $this->qrcode = $wechat->qrcode;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $addButton = sp_get_AddActionButton($this->currentRouteName);
        return view(
            'wechat.wall.index',
            ["addButton"=>$addButton]
        );
    }

    public function ajaxIndex()
    {
        $result = $this->wallBLL->ajaxIndex($this->currentRouteName);
        return response()->json($result);
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('wechat.wall.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(WallRequest $request)
    {
        $attributes = $request->all();
        $file = $request->file('background');
        if (!empty($file)) {
            $ext = $file->getClientOriginalExtension();
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            $path = $file->storeAs(
                'wallbk',
                $filename,
                'local_public'
            );
            $attributes['background'] = $path;
        }
        $this->wallBLL->createEntity($attributes);
        return redirect(route('wall.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $wall = $this->wallBLL->editView($id);
        return view(
            'wechat.wall.show',
            ["wall"=>$wall]
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $wall = $this->wallBLL->editView($id);
        return view(
            'wechat.wall.edit',
            ["wall"=>$wall]
        );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(WallRequest $request, $id)
    {
        $attributes = $request->all();
        $file = $request->file('background');
        if (!empty($file)) {
            $ext = $file->getClientOriginalExtension();
            $filename = date('Y-m-d-H-i-s') . '-' . uniqid() . '.' . $ext;
            $path = $file->storeAs(
                'wallbk',
                $filename,
                'local_public'
            );
            $attributes['background'] = $path;
        }

        $this->wallBLL->updateEntity($attributes, $id);
        return redirect(route('wall.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->wallBLL->destroyEntity($id);
        return redirect(route('wall.index'));
    }

    public function createQrCode($id)
    {
        $wall = $this->wallBLL->editView($id);
        if (empty($wall['qr_code'])) {
            $failureDate = $wall['failure_date'];
            $failureDate = $failureDate.' 23:59:59';
            $endTime = strtotime($failureDate);
            $now = time();
            $difference = $endTime - $now;
            $qrImg = $this->qrcode->temporary($id, $difference);
            //保存
            $wall['qr_code'] = $this->qrcode->url($qrImg->ticket);
            $attributes = $wall->getAttributes();
            $this->wallBLL->updateEntity($attributes, $id, "二维码产生成功", "二维码产生失败");
        } else {
            flash('二维码不能重复产生', 'error');
        }


        return redirect(route('wall.index'));
    }
}
